package com.product.web;

import com.product.entity.CategoryEntity;
import com.product.service.CategoryService;
import com.product.vo.Catelog2Vo;
import org.redisson.Redisson;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    RedissonClient redisson;
    @Autowired
    StringRedisTemplate redisTemplate;
    //这里是和MyRedissonConfig里面写的名字一样 当然redissonClient也是可以的
    @GetMapping({"/","index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Categorys();
        //视图解析器 classpath:templates/  +  返回值 +   .html
        model.addAttribute("categorys",categoryEntityList);
        return "index";
    }
    @ResponseBody
    @GetMapping("/index/catelog.json")
    public  Map<String, List<Catelog2Vo>>  getCatelogJson(){
        Map<String, List<Catelog2Vo>>  map = categoryService.getCatelogJson();
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        //1 获取一把锁，只要锁的名字一样，就是同一把锁
        RLock lock = redisson.getLock("my-lock");
        // 2 加锁
        lock.lock(); // 阻塞式等待，默认加的锁都是30s
        // 锁的自动续期，如果业务超长，运行期间自动给锁续上新的30s。不用担心业务时间长，锁自动过期被删除
        // 加锁的业务只要运行完成，就不会给当前锁续期，即使不手动解锁，锁默认在30s以后自动删除
        lock.lock(10, TimeUnit.SECONDS); //10s自动解锁，自动解锁时间一定要大于业务的执行时间
//        lock.lock(10, TimeUnit.SECONDS); // 锁时间到了之后，不会自动续期
        try {
            System.out.println("加锁成功，执行业务" + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {

        }finally {
            // 3 解锁
            System.out.println("释放锁" + Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }
    @GetMapping("/write")
    @ResponseBody
    public String writeValue() {
        //读写锁 大家可以一起读 但是如果有个人拿了写锁 那么后面的都要等待
        //也就是写 是排他 读 是共享
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.writeLock();
        try {
            rLock.lock();
            System.out.println("nihao");
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue", s);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }
    @GetMapping("/read")
    @ResponseBody
    public String readValue() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.readLock();
        try {
            rLock.lock();
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }
    @GetMapping("/park")
    @ResponseBody
    public String park() throws  InterruptedException {
        //获取一个信号 获取一个值 在这里表占车位 在redis中我设置为了3 也就是三个车位
        //如果有车位 那么就信号量减一 变成2 如果没有车位 那么就会阻塞式等待直到有车位位置
        //还有个方法是park.tryAcquire() 不是阻塞式等待 猫一眼 有就停车没有就走
        RSemaphore park = redisson.getSemaphore("park");
        park.acquire();
        return "OK";
    }

    @GetMapping("/leave")
    @ResponseBody
    public String go(){
        //释放信号量
        RSemaphore park = redisson.getSemaphore("park");
        park.release();
        return "OK";
    }
    /***
     * 放假 锁门
     * 1班没人了
     */
    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        //阻塞式等待全部人都走
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假了";
    }
    @GetMapping("/gogogo/{id}")
    public String googogo(@PathVariable("id") Long id){
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.countDown();
        return id+"班都走了";
    }


}
